package homework.no_4;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Random;

public class TCPServer {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(8800);
        serverSocket = new ServerSocket(8801);
        while (true){
            Socket socket = serverSocket.accept();

            new Thread(new Runnable() {
                @Override
                public void run() {

                    int count = 0;
                    try {
                        InputStream is = socket.getInputStream();
                        File file = new File("doc/");
                        if (!file.exists()){
                            file.mkdirs();
                        }
                        count++;
                        String ip = socket.getInetAddress().getHostAddress();
                        int port = socket.getPort();
                        System.out.println("第"+count+"个"+ip+"/"+port+"客户端成功与服务器建立连接");
                        ByteArrayOutputStream baos = cloneInputStream(is);
                        InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
                        InputStream is2 = new ByteArrayInputStream(baos.toByteArray());

                        String type = getTypeBySream(is1);
                        String fileName = "baqn"+System.currentTimeMillis()+new Random().nextInt(999)+"."+type;
                        FileOutputStream fos = new FileOutputStream(file+"\\"+fileName);
                        int len = 0;
                        byte[] bytes = new byte[1024];
                        while ((len = is1.read(bytes)) != -1){
                            fos.write(bytes,0,len);
                            socket.getOutputStream().write("上传成功".getBytes());
                        }
                        socket.close();
                        fos.close();

                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

                private String getTypeBySream(InputStream is1) {
                    byte[] bytes = new byte[1024];
                    try {
                        is1.read(bytes,0,bytes.length);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    String type = bytesToHexString(bytes).toUpperCase();
                    if(type.contains("FFD8FF")){
                        return "jpg";
                    }else if(type.contains("89504E47")){
                        return "png";
                    }else if(type.contains("47494638")){
                        return "gif";
                    }else if(type.contains("49492A00")){
                        return "tif";
                    }else if(type.contains("424D")){
                        return "bmp";
                    }else if(type.contains("52617221")){
                        return "rar";
                    }else if(type.contains("0000001c")){
                        return "MP4";
                    }
                    return type;
                }

                private String bytesToHexString(byte[] bytes) {
                    StringBuilder stringBuilder = new StringBuilder();
                    if (bytes == null || bytes.length <= 0) {
                        return null;
                    }
                    for (int i = 0; i < bytes.length; i++) {
                        int v = bytes[i] & 0xFF;
                        String hv = Integer.toHexString(v);
                        if (hv.length() < 2) {
                            stringBuilder.append(0);
                        }
                        stringBuilder.append(hv);
                    }
                    return stringBuilder.toString();
                }

                private ByteArrayOutputStream cloneInputStream(InputStream is) {

                    try {
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        byte[] bytes = new byte[1024];
                        int len = 0;
                        while ((len = is.read()) != -1){
                            baos.write(bytes,0,len);
                        }
                        baos.flush();
                        return baos;
                    } catch (IOException e) {
                        e.printStackTrace();
                        return null;
                    }
                }
            }).start();
        }
    }
}
